<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #app {
            margin: 50px auto;
            width: 600px;
        }

        fieldset {
            border: lpx solid purple;
            margin-bottom: 20px;
        }

        fieldset input {
            width: 200px;
            height: 30px;
            margin: 10px 0;
        }

        fieldset select {
            width: 200px;
            height: 30px;
            margin: 10px 0;
        }

        table {
            width: 600px;
            border: 2px solid purple;
            text-align: center;
        }

        thead {
            background-color: purple;
            color: #fff;
        }
    </style>
</head>
<!-- v-model 多选 -->

<body>
    <div id="app">
        <fieldset>
            <legend>信息录入系统</legend>
            <div>
                <label for="">
                    姓名：
                    <input type="text" placeholder="请输入姓名" v-model.trim="newPerson.name">
                </label>
            </div>
            <div>
                <label for="">
                    年龄：
                    <input type="text" placeholder="请输入年龄" v-model.trim="newPerson.age">
                </label>
            </div>
            <div>
                <label for="">
                    性别：
                    <select name="sex" id="" v-model="newPerson.sex">
                        <option value="男">男</option>
                        <option value="女">女</option>
                    </select>
                </label>
            </div>
            <div>
                <label for="">
                    地址：
                    <input type="text" placeholder="请输入地址" v-model.trim="newPerson.address">
                </label>
            </div>
            <div>
                <button @click="add">创建信息</button>
            </div>
        </fieldset>

        <div>
            <table>
                <thead>
                    <tr>
                        <td></td>
                        <td>姓名</td>
                        <td>性别</td>
                        <td>年龄</td>
                        <td>地址</td>
                        <td>删除</td>
                    </tr>
                </thead>

                <tbody>
                    <tr v-for="(p, index) in persons" :key="id">
                        <td><input type="checkbox" name="" id="" :value="id"></td>
                        <td>{{p.name}}</td>
                        <td>{{p.age}}</td>
                        <td>{{p.sex}}</td>
                        <td>{{p.address}}</td>
                        <td><button @click="del(index)">删除</button></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <script src="js/vue.js"></script>

    <script>
        const HelloVue = {
            data() {
                return {
                    persons: [
                        { id: '0001', 'name': '张三', age: 20, sex: '男', address: '武汉' },
                        { id: '0002', 'name': '李四', age: 22, sex: '男', address: '北京' },
                        { id: '0003', 'name': '王五', age: 24, sex: '男', address: '广州' },
                        { id: '0004', 'name': '赵六', age: 21, sex: '男', address: '重庆' },
                        { id: '0005', 'name': '苏小妹', age: 18, sex: '女', address: '西安' },
                    ],
                    newPerson: {
                        id: '', 'name': '', age: '', sex: '', address: ''
                    },

                }
            },
            computed: {
                filterPersons() {
                },
            },
            methods: {
                add() {
                    const {name, age, sex, address} = this.newPerson;

                    // 1. 判断
                    if(!name || !age || !sex || !address){
                        alert("输入有误");
                        return;
                    }
                   
                    // 2. 头部新增
                    this.persons.unshift(this.newPerson);

                    // 3. 清空数据
                    this.newPerson =  { id: '', 'name': '', age: '', sex: '', address: ''};
                },
                del(index) {
                    this.persons.splice(index, 1);
                }
            }
        };

        const app = Vue.createApp(HelloVue).mount("#app");
    </script>
</body>

</html>